//
//  FCPhotoBrowserTool.m
//  FCCommonUse
//
//  Created by fly on 2020/1/7.
//

#import "FCPhotoBrowserTool.h"
#import "WJCDefine.h"
//#import "LCActionSheet.h"
//#import "FCAddCollectionTool.h"
#import "UIViewController+returnBlock.h"

@interface FCPhotoBrowserTool ()

@end

@implementation FCPhotoBrowserTool

#pragma mark - 图片浏览器 只有一张图片（头像）
+ (void)photoBrowserWithImageUrl:(NSString *)imageUrl showFromVC:(UIViewController *)showFromVC
{
    NSMutableArray *photos = [NSMutableArray array];
    GKPhoto *photo = [GKPhoto new];
    photo.url = [NSURL URLWithString:imageUrl];
    [photos addObject:photo];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:0];
    browser.showStyle = GKPhotoBrowserShowStyleNone;
    browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate;
    browser.delegate = (id)showFromVC;
    
    [browser showFromVC:showFromVC];
}

#pragma mark - 图片浏览器 (多张)
+ (void)photoBrowserWithImages:(NSMutableArray *)images sourceImageViews:(NSMutableArray *)imageViews currentIndex:(NSInteger)index showFromVC:(UIViewController *)showFromVC
{
    NSMutableArray *photos = [NSMutableArray array];
    [images enumerateObjectsUsingBlock:^(id _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        GKPhoto *photo = [GKPhoto new];
        if ([obj isKindOfClass:[UIImage class]]) {
            photo.image = obj;
        } else if ([obj hasPrefix:@"http"]) {
            photo.url = [NSURL URLWithString:obj];
        } else {
            photo.image = [UIImage imageNamed:obj];
        }
        if (imageViews.count) {
            photo.sourceImageView = imageViews[idx];
        }
        [photos addObject:photo];
    }];
    
    GKPhotoBrowser *browser = [GKPhotoBrowser photoBrowserWithPhotos:photos currentIndex:index];
    if (imageViews.count) {
        browser.showStyle = GKPhotoBrowserShowStyleZoom;  // 缩放显示
    } else {
        browser.showStyle = GKPhotoBrowserShowStyleNone;
    }
    browser.hideStyle = GKPhotoBrowserHideStyleZoomScale;   // 缩放隐藏
    browser.loadStyle = GKPhotoBrowserLoadStyleDeterminate; // 明确的加载方式带进度条
    browser.isLowGifMemory = YES;
    browser.delegate = (id)showFromVC;
    
    [browser showFromVC:showFromVC];
}

#pragma mark -  图片浏览器actionSheet弹窗
- (void)photoBrowser:(GKPhotoBrowser *)browser actionSheetWithTitleArray:(NSMutableArray *)titleArray
{
//    NSString *saveImage = packageLocaChanged(@"common_emun_save_picture", @"保存图片");
//    NSString *qrCode = packageLocaChanged(@"common_emun_identify_qr_code", @"识别二维码");
//    NSString *cancel = packageLocaChanged(@"global_general_cancel", @"取消");
//
//    NSMutableArray *btnTitleArr = [NSMutableArray arrayWithObjects:saveImage, nil];
//    if (titleArray.count) {
//        btnTitleArr = titleArray;
//    }
//
//    NSString *imageUrl = [browser.curPhotoView.photo.url absoluteString];
//    UIImage *currentImage = browser.curPhotoView.photo.image;
//    NSString *messageString = [self detectorQRCodeWithImage:currentImage];
//    if (messageString.length) {
//        [btnTitleArr addObject:qrCode];
//    }
//
//    LCActionSheet *actionSheet = [LCActionSheet sheetWithTitle:nil cancelButtonTitle:cancel didDismiss:^(LCActionSheet * _Nonnull actionSheet, NSInteger buttonIndex) {
//
//        NSString *title = btnTitleArr[buttonIndex - 1];
//
//        if ([title isEqualToString:saveImage]) {
//
//             UIImageWriteToSavedPhotosAlbum(currentImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//
//        }  else if ([title isEqualToString:qrCode]) {
//
//            [browser dismiss];
//            if (self.showFromVC == nil) {
//                self.showFromVC = [FCCGetCurrentVcTool getCurrentViewController];
//            }
//            [self.showFromVC performObjbect:NSClassFromString(@"FCHandleCode") andMethod:@"analysisCode:andController:" withObjects:messageString,self.showFromVC, nil];
//        }
//
//    } otherButtonTitleArray:btnTitleArr];
//
//    [actionSheet show];
}

#pragma mark - 识别二维码
- (NSString *)detectorQRCodeWithImage:(UIImage *)image
{
    //1. 初始化扫描仪，设置设别类型和识别质量
    CIDetector *detector = [CIDetector detectorOfType:CIDetectorTypeQRCode context:nil options:@{CIDetectorAccuracy : CIDetectorAccuracyHigh}];
    
    //2. 扫描获取的特征组
    NSArray *features = [detector featuresInImage:[CIImage imageWithCGImage:image.CGImage]];
    
    //3. 获取扫描结果
    CIQRCodeFeature *feature = features.firstObject;
    
    return feature.messageString;
}


#pragma mark - 保存图片回调
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
//    error ? [SVProgressHUD showErrorWithStatus:packageLocaChanged(@"global_ios_save_failure",@"保存失败")] : [SVProgressHUD showSuccessWithStatus:packageLocaChanged(@"global_general_save_succeed", @"保存成功")];
}

@end
